package com.blog.cmrpersonalblog.task;

import com.blog.cmrpersonalblog.service.AnnouncementService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 公告清理定时任务
 * 负责自动过期公告和清理过期数据
 */
@Slf4j
@Component
public class AnnouncementCleanupTask {

   @Resource
    private AnnouncementService announcementService;

    /**
     * 自动过期公告
     * 每小时执行一次，检查并过期到期的公告
     */
    @Scheduled(cron = "0 0 * * * ?")
    public void autoExpireAnnouncements() {
        try {
            log.info("开始执行公告自动过期任务");
            int expiredCount = announcementService.autoExpireAnnouncements();
            if (expiredCount > 0) {
                log.info("公告自动过期任务完成，过期公告数量: {}", expiredCount);
            } else {
                log.debug("公告自动过期任务完成，无需过期的公告");
            }
        } catch (Exception e) {
            log.error("公告自动过期任务执行失败", e);
        }
    }

    /**
     * 清理已删除的公告
     * 每天凌晨3点执行，清理30天前删除的公告（物理删除）
     */
    @Scheduled(cron = "0 0 3 * * ?")
    public void cleanDeletedAnnouncements() {
        try {
            log.info("开始执行已删除公告清理任务");
            int cleanedCount = announcementService.cleanDeletedAnnouncements(30);
            if (cleanedCount > 0) {
                log.info("已删除公告清理任务完成，清理数量: {}", cleanedCount);
            } else {
                log.debug("已删除公告清理任务完成，无需清理的公告");
            }
        } catch (Exception e) {
            log.error("已删除公告清理任务执行失败", e);
        }
    }

    /**
     * 清理过期的阅读记录
     * 每周日凌晨4点执行，清理90天前的阅读记录
     */
    @Scheduled(cron = "0 0 4 * * 0")
    public void cleanExpiredReadRecords() {
        try {
            log.info("开始执行过期阅读记录清理任务");
            int cleanedCount = announcementService.cleanExpiredReadRecords(90);
            if (cleanedCount > 0) {
                log.info("过期阅读记录清理任务完成，清理数量: {}", cleanedCount);
            } else {
                log.debug("过期阅读记录清理任务完成，无需清理的记录");
            }
        } catch (Exception e) {
            log.error("过期阅读记录清理任务执行失败", e);
        }
    }

    /**
     * 生成公告统计报告
     * 每天凌晨1点执行，生成公告统计信息并记录日志
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void generateAnnouncementStatistics() {
        try {
            log.info("开始生成公告统计报告");
            var statistics = announcementService.getAnnouncementStatistics();
            
            log.info("=== 公告统计报告 ===");
            log.info("总公告数: {}", statistics.get("total_count"));
            log.info("草稿数: {}", statistics.get("draft_count"));
            log.info("已发布数: {}", statistics.get("published_count"));
            log.info("已过期数: {}", statistics.get("expired_count"));
            log.info("已删除数: {}", statistics.get("deleted_count"));
            log.info("置顶数: {}", statistics.get("top_count"));
            log.info("弹窗数: {}", statistics.get("popup_count"));
            log.info("总浏览量: {}", statistics.get("total_view_count"));
            log.info("=== 统计报告结束 ===");
            
        } catch (Exception e) {
            log.error("生成公告统计报告失败", e);
        }
    }
}
